﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace engine
{
    public class ScreenManager
    {
        public ScreenManager()
        {
            screenTransitions = new List<ScreenTransition>();
            screens = new List<Screen>();
        }

        private List<ScreenTransition> screenTransitions;
        private List<Screen> screens;
        private Screen activeScreen;

        public void AddTransition(ScreenTransition trans)
        {
            screenTransitions.Add(trans);
        }

        public void AddScreen(Screen screen, bool loaded)
        {
            if (!loaded)
            {
                screen.Initialize(Engine.Instance.Graphics.Viewport.AspectRatio);
                screen.LoadContent(Engine.Instance.Game.Services);
            }

            if (!screens.Contains(screen))
            {
                screens.Add(screen);
            }
        }

        public void RemoveScreen(Screen screen, bool unload)
        {
            if (unload)
            {
                screen.UnloadContent();
            }

            screens.Remove(screen);
        }

        /// <summary>
        /// Screen must be added previously with AddScreen().
        /// </summary>
        /// <param name="screen"></param>
        /// <returns></returns>
        public Screen SetActiveScreen(Screen screen)
        {
            if (screens.Contains(screen))
            {
                Screen old = activeScreen;
                activeScreen = screen;
                return old;
            }
            return null;
        }

        public bool Update()
        {
            if (activeScreen != null)
            {
                ScreenResult result = activeScreen.GetState();
                if (result != null)
                {
                    return ProcessResult(result);
                }
            }
            return false;
        }

        private bool ProcessResult(ScreenResult result)
        {
            foreach (ScreenTransition trans in screenTransitions)
            {
                if (trans.ScreenFrom == activeScreen && trans.ScreenResult == result)
                {
                    if (screens.Contains(trans.ScreenTo))
                    {
                        SetActiveScreen(trans.ScreenTo);
                        return true;
                    }
                    return false;
                }
            }
            return false;
        }

        public void UpdateScreen(GameTime gameTime)
        {
            if (activeScreen != null)
            {
                activeScreen.Update(gameTime);
            }
        }

        public void DrawScreen(GameTime gameTime)
        {
            if (activeScreen != null)
            {
                activeScreen.Draw(gameTime);
            }
        }
    }
}
